﻿Imports Microsoft.Windows.Shell

Public Class ChromelessWindow
    Inherits Window



    Public Property SystemWindowBorderWidth As Double
        Get
            Return DirectCast(GetValue(SystemWindowBorderWidthProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(SystemWindowBorderWidthProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SystemWindowBorderWidthProperty As DependencyProperty = DependencyProperty.Register( _
        "SystemWindowBorderWidth", GetType(Double), GetType(ChromelessWindow), _
        New FrameworkPropertyMetadata(0.0, _
            Sub(d, e) DirectCast(d, ChromelessWindow).OnSystemWindowBorderWidthChanged( _
                DirectCast(e.OldValue, Double), DirectCast(e.NewValue, Double))), _
        Function(o) True)

    Protected Overridable Sub OnSystemWindowBorderWidthChanged(oldValue As Double, newValue As Double)
        ResizeBorderThickness = New Thickness(newValue + 7)
    End Sub



    Public Property ResizeBorderThickness As Thickness
        Get
            Return DirectCast(GetValue(ResizeBorderThicknessProperty), Thickness)
        End Get
        Set(ByVal value As Thickness)
            SetValue(ResizeBorderThicknessProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ResizeBorderThicknessProperty As DependencyProperty = DependencyProperty.Register( _
        "ResizeBorderThickness", GetType(Thickness), GetType(ChromelessWindow), _
            New FrameworkPropertyMetadata(New Thickness(7)))




    Public Property CaptionHeight As Double
        Get
            Return DirectCast(GetValue(CaptionHeightProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(CaptionHeightProperty, value)
        End Set
    End Property

    Public Shared ReadOnly CaptionHeightProperty As DependencyProperty = DependencyProperty.Register( _
        "CaptionHeight", GetType(Double), GetType(ChromelessWindow), _
            New FrameworkPropertyMetadata(64.0))



    Public Property CornerRadius As CornerRadius
        Get
            Return DirectCast(GetValue(CornerRadiusProperty), CornerRadius)
        End Get
        Set(ByVal value As CornerRadius)
            SetValue(CornerRadiusProperty, value)
        End Set
    End Property

    Public Shared ReadOnly CornerRadiusProperty As DependencyProperty = DependencyProperty.Register( _
        "CornerRadius", GetType(CornerRadius), GetType(ChromelessWindow), _
            New FrameworkPropertyMetadata(New CornerRadius(0)))



    Private MinimizeCommandBinding As New CommandBinding(
        SystemCommands.MinimizeWindowCommand,
        Sub(s, e) SystemCommands.MinimizeWindow(Me))

    Private MaximizeCommandBinding As New CommandBinding(
        SystemCommands.MaximizeWindowCommand,
        Sub(s, e) SystemCommands.MaximizeWindow(Me),
        Sub(s, e) e.CanExecute = Me.WindowState <> Windows.WindowState.Maximized)

    Private RestoreCommandBinding As New CommandBinding(
        SystemCommands.RestoreWindowCommand,
        Sub(s, e) SystemCommands.RestoreWindow(Me),
        Sub(s, e) e.CanExecute = Me.WindowState = Windows.WindowState.Maximized)

    Private CloseCommandBinding As New CommandBinding(
        SystemCommands.CloseWindowCommand,
        Sub(s, e) SystemCommands.CloseWindow(Me))

    Private SysMenuCommandBinding As New CommandBinding(
        SystemCommands.ShowSystemMenuCommand,
        Sub(s, e) SystemCommands.ShowSystemMenu(Me, PointToScreen(New Point(0, 0))))



    Public Sub New()

        SetResourceReference(SystemWindowBorderWidthProperty, SystemParameters.BorderWidthKey)

        Dim c As New WindowChrome
        c.GlassFrameThickness = New Thickness(1, 0, 0, 0)
        BindingOperations.SetBinding(c, _
                                     WindowChrome.CaptionHeightProperty, _
                                     New Binding With {.Source = Me, .Path = New PropertyPath(CaptionHeightProperty)})
        BindingOperations.SetBinding(c, _
                                     WindowChrome.CornerRadiusProperty, _
                                     New Binding With {.Source = Me, .Path = New PropertyPath(CornerRadiusProperty)})
        BindingOperations.SetBinding(c, _
                                     WindowChrome.ResizeBorderThicknessProperty, _
                                     New Binding With {.Source = Me, .Path = New PropertyPath(ResizeBorderThicknessProperty)})

        WindowChrome.SetWindowChrome(Me, c)

        RenderOptions.SetClearTypeHint(Me, ClearTypeHint.Enabled)

        CommandBindings.Add(MinimizeCommandBinding)
        CommandBindings.Add(MaximizeCommandBinding)
        CommandBindings.Add(RestoreCommandBinding)
        CommandBindings.Add(CloseCommandBinding)
        CommandBindings.Add(SysMenuCommandBinding)

        AddHandler StateChanged, Sub(d, e) CommandManager.InvalidateRequerySuggested()

    End Sub
End Class
